<template>
    <div class="home-container">
      <!-- 导航栏 -->
        <van-nav-bar class="page-nav-bar" fixed>
        <van-button slot="title" type="info" round size="small" icon="search" class="search-btn" to="/search">搜索</van-button>
        </van-nav-bar>
      <!-- 导航栏 -->

      <!-- 频道列表  animated 动画切换  swipeable 滑动效果-->
        <van-tabs class="channel-tabs" v-model="active" animated swipeable>
          <van-tab :title="obj.name" v-for="obj in channels" :key="obj.id">
            <ArticleList :channel="obj"></ArticleList>
          </van-tab>
          <div slot="nav-right" class="placeheder"></div>
          <div slot="nav-right" class="hamburger">
            <i class="iconfont icon-gengduo" @click="isShowEdit=true"></i>
          </div>
        </van-tabs>
      <!-- 频道列表 -->

      <!-- 频道编辑弹出层 -->
      <van-popup
        v-model="isShowEdit"
        close-icon-position="top-left"
        closeable
        position="bottom"
        :style="{ height: '100%' }"
      >
      <ChannelEdit :channel="channels" :active="active" @active-update="update" ></ChannelEdit>
      </van-popup>
       <!--/ 频道编辑弹出层 -->
    </div>
</template>

<script>
import { getUserChannels } from '@/api/user'
import ArticleList from '@/views/home/components/article-list'
import ChannelEdit from '@/views/home/components/channel-edit'
import { mapState } from 'vuex'
import { getItem } from '@/uitls/storage'
export default {
  name: 'HomeIndex',
  components: {
    ArticleList,
    ChannelEdit
  },
  computed: {
    ...mapState(['user'])
  },
  data () {
    return {
      active: 0,
      channels: [], // 频道列表
      isShowEdit: false // 控制弹出层
    }
  },
  created () {
    this.loadChannels()
  },
  methods: {
    async loadChannels () {
      // 1.加载中，将我的频道数据写入
      // 2.俩种情况， 登录，未登录
      try {
        let channel = []
        if (this.user) { // 如果用户登录
          const { data } = await getUserChannels()
          channel = data.data.channels
        } else { // 用户未登录
          const localChannel = getItem('CHANNELS')
          if (localChannel) { // 本地存储有数据
            channel = localChannel
          } else { // 本地存储没有数据 使用默认数据
            const { data } = await getUserChannels()
            channel = data.data.channels
          }
        }
        this.channels = channel
      } catch (err) {
        this.$toast('获取我的频道数据失败，请重试')
        console.log(err)
      }
    },
    update (i, status = true) { // 跳转
      this.isShowEdit = status
      this.active = i
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  padding-bottom: 100px;
  padding-top: 92px;
  .van-nav-bar__title{
    max-width: unset;
  }
  //作用不到根节点 /deep/
  .search-btn {
    width: 555px;
    height: 64px;
    max-width: 100%;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }

  /deep/ .channel-tabs{
    position: relative;
    .van-tabs__nav {
      height: 82px;
      padding-bottom: 0;
      position: fixed;
      top: 92px;
      z-index: 2;
      width: 100%;
    }
    .van-tab {
      min-width: 185px;
      box-sizing: border-box;
      border-right: 1px solid #edeff3;
      font-size: 30px;
      color: #777;
    }
    .van-tab--active{
      color: #333;
    }
    .van-tabs__line{
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
      bottom: 8px;
    }
    // flex-shrink: 0; 参与flex布局的计算
    .placeheder{
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }
    .hamburger{
      width: 66px;
      height: 82px;
      position: fixed;
      display: flex;
      justify-content: center;
      align-items: center;
      right: 0;
      background-color: #fff;
      opacity: 0.902;
      i.iconfont {
        font-size: 33px;
      }
      &::before {
        content: "";
        position: absolute;
        left: 0;
        width: 1px;
        height: 100%;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
}

</style>
